﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YasharEl.Infrastructure.ApplicationModel.Bootstrap.Extensibility;
using System.Configuration;
using YasharEl.Infrastructure.ApplicationModel.Bootstrap.Configuration;
using System.Web;
using System.Web.Configuration;

namespace YasharEl.Infrastructure.ApplicationModel.Modularity.Bootstrap
{
    public abstract class ModuleComponentBootstrappingExtension
        : BaseComplexBootstrappingExtension, IModuleComponentBootstrappingExtension,
          ILoadConfigurationSection, IHaveConfigurationSectionName
    {
        #region Fields

        private System.Configuration.Configuration _moduleConfiguration = null;

        #endregion

        #region Properties

        public string ModuleName
        {
            get;
            set;
        }

        #endregion

        #region IModuleComponentBootstrappingExtension Implementation

        public virtual System.Configuration.Configuration ModuleConfiguration
        {
            get
            {
                if (_moduleConfiguration != null)
                    return _moduleConfiguration;

                // Get the current configuration file.
                if (HttpContext.Current != null)
                    return WebConfigurationManager.OpenWebConfiguration("~");
                else
                    return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            }
            set
            {
                _moduleConfiguration = value;
            }
        }

        #endregion

        #region ILoadConfigurationSection Members

        public ConfigurationSection GetSection(string sectionName)
        {
            return ModuleConfiguration.GetSection(sectionName);
        }

        #endregion

        #region IHaveConfigurationSectionName Members - Still Abstract

        public abstract string SectionName
        {
            get;
        }

        #endregion
    }
}
